package com.travelDevelop.service;

import java.nio.charset.Charset;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.travelDevelop.commons.bean.ChatInfo;
import com.travelDevelop.commons.bean.ChatInfoExample;
import com.travelDevelop.commons.util.UUIDUtil;
import com.travelDevelop.dao.ChatInfoMapper;


@Service
public class ChatInfoService {

	@Autowired
	public ChatInfoMapper chatInfoMapper;
	
	public void insertChatInfo(String userId,String username,String userDisplayName,String msg,long time,String courseId){
		ChatInfo chatInfo = new ChatInfo();
		chatInfo.setId(UUIDUtil.createUUID());
		chatInfo.setSendUserId(userId);
		chatInfo.setSendUsername(username);
		chatInfo.setCourseId(courseId);
		chatInfo.setSendUserDisplayName(userDisplayName);
		chatInfo.setChatTime(time);
		chatInfo.setChatInfo(msg.getBytes(Charset.forName("utf-8")));
		chatInfoMapper.insert(chatInfo);
	}

	public List<ChatInfo> getChatInofPre(int number, long time,String courseId) {
		ChatInfoExample chatInfoExample = new ChatInfoExample();
		ChatInfoExample.Criteria criteria = chatInfoExample.createCriteria();
		criteria.andCourseIdEqualTo(courseId);
		criteria.andChatTimeLessThan(time);
		chatInfoExample.setOffset(0);
		chatInfoExample.setLimit(number);
		chatInfoExample.setOrderByClause(" chat_time desc ");
		return chatInfoMapper.selectByExampleWithBLOBs(chatInfoExample);
	}
	
	public List<ChatInfo> getChatInfo(long startTime, long endTime,String courseId) {
		ChatInfoExample chatInfoExample = new ChatInfoExample();
		ChatInfoExample.Criteria criteria = chatInfoExample.createCriteria();
		criteria.andChatTimeBetween(startTime, endTime);
		criteria.andCourseIdEqualTo(courseId);
		chatInfoExample.setOrderByClause(" chat_time desc ");
		return chatInfoMapper.selectByExampleWithBLOBs(chatInfoExample);
	}
	
	/**
	 * 查询分组好的成员id  例如G1，G2，G3....A8...
	 * @param id ：G2 = 2
	 * @return 成员id
	 */
	public String getMembesById(Integer id) {
		return chatInfoMapper.selectMembesById(id);
	}
	
}
